<template>
  <a-modal
    v-model:open="open"
    :title="'离线部署'"
    centered
    :after-close="reset"
    :confirm-loading="confirmLoading"
    :ok-text="'创建'"
    @ok="onOk"
  >
    <a-form ref="formRef" :model="formState" :rules="rules" layout="vertical">
      <a-form-item name="name" label="服务名称">
        <a-input v-model:value="formState.name" placeholder="请输入服务名称" :maxlength="200" />
      </a-form-item>
      <a-form-item name="type" label="部署类型">
        <a-input v-model:value="formState.type" placeholder="请输入服务名称" :maxlength="200" />
      </a-form-item>
      <a-form-item name="description" label="描述">
        <a-textarea v-model:value="formState.description" placeholder="请输入描述" :maxlength="200" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
  import type { FormInstance } from 'ant-design-vue'
  import type { Rule } from 'ant-design-vue/es/form'
  import { cloneDeep } from 'g6-fn'
  import { computed, ref } from 'vue'

  import { mirrorManageApi } from '@/services/api'
  import { validateBitLen, validateDirName } from '@/utils'

  export interface Props {
    afterCloseFn?: () => void
  }

  const props = withDefaults(defineProps<Props>(), {
    afterCloseFn: () => {}
  })

  interface FormState {
    id?: string
    name: string
    type: string
    description: string
  }

  const open = ref(false)
  const formRef = ref<FormInstance>()
  const formState = ref<FormState>({
    name: '',
    type: '',
    description: ''
  })

  const rules: Record<string, Rule[]> = {
    name: [
      { required: true, trigger: 'change', message: '请输入服务名称' },
      { validator: validateDirName('服务名称') },
      { validator: validateBitLen(200), trigger: 'change' }
    ]
  }
  const confirmLoading = ref(false)
  const onOk = async () => {
    await formRef.value?.validateFields()
    // confirmLoading.value = true
    console.log('表单', mirrorManageApi)
  }

  const reset = () => {
    formRef.value?.resetFields()
    formState.value = {
      name: '',
      type: '',
      description: ''
    }
  }

  defineExpose({
    open: () => {
      open.value = true
    }
  })
</script>

<style lang="less" scoped>
  .dress-input {
    :deep(input) {
      border-radius: 0 6px 6px 0 !important;
    }
  }
</style>
